package top.kaoshanji.ch04.constructor;

import java.util.Random;

/**
 * author kaoshanji
 * ^_^
 * create time 2024/11/25 16:40
 */
public class Employee {

    private static int nextId;

    private int id;
    private String name = ""; // 初始化实例字段
    private double salary;

    // 静态初始化块...在类第一次加载时执行
    static {
        var generator = new Random();
        nextId = generator.nextInt(10000);
    }

    // 实例初始化块...在构造器执行之前执行
    {
        id = nextId;
        nextId++;
    }

    // 构造器是第三种初始化实例字段的方式
    public Employee(String n, double s) {
        name = n;
        salary = s;
    }

    public Employee(double s) {
        this("Employee #" + nextId, s);
    }

    //默认的构造器
    // name 初始化的值为 ""
    public Employee() {}

    public double getSalary() {
        return salary;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}
